package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.CommentMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.KnowledgeMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.KnowledgeTagMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.Comment;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.Knowledge;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.KnowledgeTag;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.IamKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.adm.impl
 * @Description: ${TODO}(知识库服务实现)
 * @author: lutianbo
 * @date: 2017/2/16,0016
 */

public class IamKnowledgeServiceImpl implements IamKnowledgeService
{
    @Autowired
    private KnowledgeMapper knowledgeMapper;
    @Autowired
    private KnowledgeTagMapper tagMapper;
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 删除
     * @param knowledgeIds 知识库id数组
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] knowledgeIds) {
        if(knowledgeIds.length>0){
           return knowledgeMapper.deleteByPrimaryKey(knowledgeIds);
        }
        return 0;
    }

    /**
     * 新增
     * @param record 知识库实体
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(Knowledge record) {
        // 数据保存成功之后，返回的条数
        int resultCount = 0;

        // 判断数据是否为空
        if(null == record){
            return 0;
        }
        resultCount +=   knowledgeMapper.insertSelective(record);
        // 若标签为空则返回
        if(null == record.getKnowledgeTag()){
            return  resultCount;
        }
        // 分割标签
        String[] tags = record.getKnowledgeTag().split(";");
        // 标签
        KnowledgeTag knowledgeTag = new KnowledgeTag();
        // 设置知识库id
        knowledgeTag.setKnowledgeId(record.getKnowledgeId());
        // 若标签个数大于0，则分次保存
        if(tags.length>0){
            for(int i=0;i<tags.length;i++){
                // 设置主键
                knowledgeTag.setTagId(UniqueUtil.uuid());
                // 设置标签内容
                knowledgeTag.setKnowledgeTag(tags[i]);
                // 保存标签
                resultCount += tagMapper.insertSelective(knowledgeTag);
            }
        }

        return resultCount;
    }

    /**
     * 根据id查询信息
     * @param knowledgeId 知识库id
     * @return
     */
    @Override
    public Knowledge selectByPrimaryKey(String knowledgeId) {
        if(!"".equals(knowledgeId)){
          return   knowledgeMapper.selectByPrimaryKey(knowledgeId);
        }
        return null;
    }

    /**
     * 更新
     * @param record
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(Knowledge record) {
        // 数据保存成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return 0;
        }
        resultCount +=  knowledgeMapper.updateByPrimaryKeySelective(record);
        // 若标签为空则返回
        if(null == record.getKnowledgeTag()){
            return  resultCount;
        }
        // 分割标签
        String[] tags = record.getKnowledgeTag().split(";");
        // 标签
        KnowledgeTag knowledgeTag = new KnowledgeTag();
        // 设置知识库id
        knowledgeTag.setKnowledgeId(record.getKnowledgeId());
        // 若标签个数大于0，则分次保存
        if(tags.length>0){
            // 删除已有标签
            resultCount += tagMapper.deleteByKnowledgeId(record.getKnowledgeId());
            for(int i=0;i<tags.length;i++){
                // 设置主键
                knowledgeTag.setTagId(UniqueUtil.uuid());
                // 设置标签内容
                knowledgeTag.setKnowledgeTag(tags[i]);
                // 保存标签
                resultCount += tagMapper.insertSelective(knowledgeTag);
            }
        }
        return resultCount;
    }

    /**
     * 知识库列表
     * @param knowledge
     * @return
     */
    @Override
    public JSONObject list(Knowledge knowledge) {
        JSONObject jsonObject = new JSONObject();
        knowledge.setLimit( knowledge.getLimit() == null ? Integer.valueOf(10) : knowledge.getLimit() );
        knowledge.setOffset( knowledge.getOffset() == null ? Integer.valueOf(0) : knowledge.getOffset() );
        jsonObject.put("total",knowledgeMapper.queryCount(knowledge));
        jsonObject.put("rows", JSON.toJSON(knowledgeMapper.queryList(knowledge)));
        return jsonObject;
    }

    /**
     * 评论列表
     * @param comment
     * @return
     */
    @Override
    public  JSONObject queryComment(Comment comment){
        JSONObject jsonObject = new JSONObject();
        comment.setLimit( comment.getLimit() == null ? Integer.valueOf(10) : comment.getLimit() );
        comment.setOffset( comment.getOffset() == null ? Integer.valueOf(0) : comment.getOffset() );
        jsonObject.put("total",commentMapper.queryCount(comment));
        jsonObject.put("rows", JSON.toJSON(commentMapper.queryList(comment)));
        return jsonObject;
    }
}
